//
//  SDTwoAdvertisingView.m
//  Sydeny
//
//  Created by Rainbow on 7/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDTwoAdvertisingView.h"

#import "UtilHelper.h"
#import "Advertising.h"
#import "ITSGetImageForButtonOperation.h"
#import "Advertising.h"

@implementation SDTwoAdvertisingView
@synthesize advertisings,delegate;

-(id)initWithAds:(NSDictionary *)ads
{
    self = [super initWithFrame:CGRectMake(0, 0, 300, 144)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.advertisings = ads;
        operationQueue = [[NSOperationQueue alloc] init];
        [self loadComponents];
    }
    return self;
}


-(void)updateAds:(NSDictionary *)ads
{
    self.advertisings = ads;
    Advertising *advertising = [self.advertisings objectForKey:@"0"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [firstAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:firstAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
    advertising = [self.advertisings objectForKey:@"1"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [secondAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:secondAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
}

-(void)loadComponents
{
    
    firstAdButton = [[ImageButton alloc] initWithFrame:CGRectMake(0, 10, 144, 144)];
    if (threeAd) {
        
        firstAdButton.frame = CGRectMake(0, 10, 145, 60);
        [firstAdButton setBackgroundImage:[UIImage imageNamed:@"290_120.png"] forState:UIControlStateNormal];
        secondAdButton.frame = CGRectMake(155, 10, 145, 60);
        [secondAdButton setBackgroundImage:[UIImage imageNamed:@"290_120.png"] forState:UIControlStateNormal];
    }
    else
    {
        [firstAdButton setBackgroundImage:[UIImage imageNamed:@"288_288.png"] forState:UIControlStateNormal];
    }
    [firstAdButton addTarget:self action:@selector(tapOnAdvertising:) forControlEvents:UIControlEventTouchUpInside];
    firstAdButton.layer.cornerRadius = 6;
    firstAdButton.layer.masksToBounds = YES;
    firstAdButton.tag = 1;
    [self addSubview:firstAdButton];
    Advertising *advertising = [self.advertisings objectForKey:@"0"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [firstAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:firstAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
    
    secondAdButton = [[ImageButton alloc] initWithFrame:CGRectMake(156, 10, 144, 144)];
    if (threeAd) {
        
        secondAdButton.frame = CGRectMake(155, 10, 145, 60);
        [secondAdButton setBackgroundImage:[UIImage imageNamed:@"290_120.png"] forState:UIControlStateNormal];
    }
    else
    {
        [secondAdButton setBackgroundImage:[UIImage imageNamed:@"288_288.png"] forState:UIControlStateNormal];
    }
    [secondAdButton addTarget:self action:@selector(tapOnAdvertising:) forControlEvents:UIControlEventTouchUpInside];
    secondAdButton.layer.cornerRadius = 6;
    secondAdButton.layer.masksToBounds = YES;
    secondAdButton.tag = 2;
    [self addSubview:secondAdButton];
    advertising = [self.advertisings objectForKey:@"1"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [secondAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:secondAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
    
}

-(void)tapOnAdvertising:(UIButton *)btn
{
    NSInteger tag = btn.tag;
    Advertising *advertising = [self.advertisings objectForKey:[NSString stringWithFormat:@"%d", tag-1]];
    if ([delegate respondsToSelector:@selector(AdvertisingDidSelectedWithShop:)]) {
        [delegate AdvertisingDidSelectedWithShop:advertising.shop];
    }
}

- (void)dealloc
{
    [firstAdButton release];
    firstAdButton = nil;
    [secondAdButton release];
    secondAdButton = nil;
    [advertisings release];
    operationQueue = nil;
    [operationQueue release];
    [super dealloc];
}


@end
